/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "CppLangTypeSourceVisitor.h"
#include "CppLangTypeNameVisitor.h"
#include <fstream>

CppLangTypeSourceVisitor::CppLangTypeSourceVisitor(
    const std::string& outputDir, 
    const std::string& fileName, 
    CppLangTypeNameVisitor* typeNameVisitor,
    CppLangTypeHeaderVisitor* headerVisitor)
    : FileGenVisitor(outputDir, fileName),
      typeNameVisitor_(typeNameVisitor),
      headerVisitor_(headerVisitor) {
}

void CppLangTypeSourceVisitor::visit(SpecificationNode& node) {
    std::string headerFile = headerVisitor_->getFileName();
    // 文件头的宏声明
    std::string headers = "#include \"" + headerFile + "\"\n";
    content_ += headers + "\n";

    // __cplusplus宏定义开始
    content_ += "#ifdef __cplusplus\n";
    content_ += "extern \"C\" {\n";
    content_ += "#endif\n\n";

    // 递归访问子节点
    if (node.definitions_) {
        node.definitions_->accept(*this);
    }

    // __cplusplus宏定义结尾
    content_ += "#ifdef __cplusplus\n";
    content_ += "}\n";
    content_ += "#endif\n";

    // 将_content写入文件
    writeToFile();
}
